package com.meevle.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.HibernateException;
import org.red5.core.ApplicationSettings;

import com.meevle.business.SetDelegate;
import com.meevle.business.SlotDelegate;
import com.meevle.business.UserDelegate;
import com.meevle.vo.SetVO;
import com.meevle.vo.SlotVO;
/**
 * Servlet implementation class Index
 */
public class Community extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Map<Object,Object> pages = ApplicationServletSettings.getInstance().getCommunityPages();
    private String firstPage = ApplicationServletSettings.getInstance().getFirstCommunityPage(); 
    
//    private int totalNumberOfItems = SlotDelegate.getAllSlots().size();
    private int totalNumberOfItems = SetDelegate.getAllSets().size();
    private int numberOfItems = 5;
    private String serverURL = ApplicationSettings.getInstance().getServerUrl()+ApplicationSettings.getInstance().getContextPath().replace('\\', '/');
    private List<SlotVO> slots = new ArrayList<SlotVO>(numberOfItems);
    private List<SetVO> sets = new ArrayList<SetVO>(numberOfItems);
    private int[] indices = new int[(totalNumberOfItems%numberOfItems==0?totalNumberOfItems/numberOfItems:(totalNumberOfItems/numberOfItems) + 1)];

    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Community() {
        super();
    }

    protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
    	response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try
        {        
        	
        	String logUser = request.getParameter("logUserName");
        	String logPass = request.getParameter("logPassword");
        	
        	if(logUser!=null && logPass != null && UserDelegate.login(logUser, logPass))
        	{
        		HttpSession sess = request.getSession();
        		sess.setAttribute("userName", logUser);
        	}
        	
//        	handleRequest(request);
        	
        	// Get all the tracks so far
			try {
				if(request.getParameter("changeCounter")!=null) {
//					slots = SlotDelegate.getAllSlotsFromTo( (Integer.parseInt(request.getParameter("changeCounter"))-1)*numberOfItems, numberOfItems, "id");
					sets = SetDelegate.getAllSetsFromTo( (Integer.parseInt(request.getParameter("changeCounter"))-1)*numberOfItems, numberOfItems, "id");
				}
				
				else {
//					slots = SlotDelegate.getAllSlotsFromTo( 0, numberOfItems, "id");
					sets = SetDelegate.getAllSetsFromTo( 0, numberOfItems, "id");
				}
			} catch (HibernateException e) {
				;
			} catch (Exception e) {
				;
			}
			
			for(SlotVO slot : slots)
				slot.setDirectory(serverURL + slot.getDirectory().replace('\\', '/'));
			
			for(int i=0; i<indices.length; i++)
			{
				indices[i] = i+1;
			}
        	
			System.out.println("session username: " + request.getSession().getAttribute("userName"));
			
        	request.setAttribute("scripts", ApplicationServletSettings.getInstance().getScripts());
        	request.setAttribute("cssFiles", ApplicationServletSettings.getInstance().getCssFiles());
        	request.setAttribute("firstPage", firstPage);
        	request.setAttribute("navigation", ApplicationServletSettings.getInstance().getNavigationFactory().getNavigation(this));
			request.setAttribute("sidebar", ApplicationServletSettings.getInstance().getSidebarFactory().getSidebar(this, this.pages, 
					(request.getParameter("page")==null?firstPage:request.getParameter("page"))
					));
        	
			request.setAttribute("slots", slots);
			request.setAttribute("sets", sets);
			request.setAttribute("numberOfItems",  numberOfItems);
			request.setAttribute("totalNumberOfItems", totalNumberOfItems);
			request.setAttribute("indices", indices);
			
			RequestDispatcher view = request.getRequestDispatcher("com/meevle/web/pages/Community.jsp");
			view.forward(request, response);
        }
        finally
        {
        	out.close();
        }
		
	}

    private void handleRequest(HttpServletRequest request) {
		
	}
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}
}
